package com.gmm.springboot.admin.mapper.test02;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import com.gmm.common.StringUtil;

public class LoginRecordSqlProvider {
	public String getSelectRecordSql(Map<String, Object> parameter) {

		return new SQL() {
			{
				SELECT("*");
				FROM("t_login_record");

				if (parameter.get("id") != null) {
					WHERE("id=#{id}");
				}
				if (parameter.get("userId") != null) {
					WHERE("user_id=#{userId}");
				}
				if (parameter.get("beginLoginTime") != null) {
					WHERE("login_time>=#{beginLoginTime}");
				}
				if (parameter.get("endLoginTime") != null) {
					WHERE("login_time<=#{endLoginTime}");
				}
				ORDER_BY("id desc");

			}
		}.toString() + " limit #{beginIndex}, #{maxCount}";

	}

	public String getSelectCountSql(Map<String, Object> parameter) {

		return new SQL() {
			{
				SELECT("count(*)");
				FROM("t_login_record");

				if (parameter.get("id") != null) {
					WHERE("id=#{id}");
				}
				if (parameter.get("userId") != null) {
					WHERE("user_id=#{userId}");
				}
				if (parameter.get("beginLoginTime") != null) {
					WHERE("login_time>=#{beginLoginTime}");
				}

				if (parameter.get("endLoginTime") != null) {
					WHERE("login_time<=#{endLoginTime}");
				}
			}
		}.toString();

	}

	public String getUpdateSql(LoginRecord record) {

		return new SQL() {
			{
				UPDATE("t_login_record");

				if (record.getUserId() != null) {
					SET("user_id=#{userId}");
				}
				if (StringUtil.isNotBlank(record.getIp())) {
					SET("ip = #{ip}");
				}
				if (record.getLoginTime() != null) {
					SET("login_time=#{loginTime}");
				}
				WHERE("id=#{id}");
			}
		}.toString();

	}

}
